#include "narrationwidgettool.h"
#include "ui_narrationwidgettool.h"

NarrationWidgetTool::NarrationWidgetTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NarrationWidgetTool)
{
    ui->setupUi(this);
    nw = new NarrationWidget(this);
    ui->ToolWidget->move(0,0);
    nw->move(0,ui->ToolWidget->size().height() + paddingtop);
    nw->resize(this->width(),this->height() - ui->ToolWidget->height() - paddingtop);
    InitBtns();
}

NarrationWidgetTool::~NarrationWidgetTool()
{
    delete ui;
    delete nw;
}

void NarrationWidgetTool::resizeEvent(QResizeEvent *event)
{
    nw->resize(this->width(),this->height() - ui->ToolWidget->height() - paddingtop);
}

void NarrationWidgetTool::InitBtns()
{
    connect(ui->BackBtn,&QPushButton::clicked,nw,[=](){nw->Back();});
    connect(ui->AddBtn,&QPushButton::clicked,this,[=](){
        NarrationNode* node = new NarrationNode();
        if(GameManager::Instance().curNode)
        {
            if(GameManager::Instance().curNode->childId >= 0)
            {
                NarrationNode* child = NarrationTree::Instance().narrationTree[GameManager::Instance().curNode->childId];
                child->parentId = node->id;
                node->childId = child->id;
            }
            node->parentId = GameManager::Instance().curNode->id;
            GameManager::Instance().curNode->childId = node->id;
        }
        emit EventCenter::Instance().AddNode(node->id);
    });
    connect(ui->NameBtn,&QPushButton::clicked,this,[=](){
        nw->ShowEdit();
        qDebug()<<GameManager::Instance().curNode->id<<"的子节点是"<<GameManager::Instance().curNode->childId<<"的父节点是"<<GameManager::Instance().curNode->parentId;
    });
    connect(ui->DeleteBtn,&QPushButton::clicked,this,[=](){
        int rows = nw->count();
        int curRow = nw->currentRow();
        if(curRow == 0 && rows > 1)
        {
            nw->setCurrentRow(1);
        }
        else if(curRow > 0)
            nw->setCurrentRow(curRow - 1);
        nw->DeleteNodeItem(curRow);
    });
}
